<template>
  <div class="order">
    <v-navbar></v-navbar>
    <div class="order-wrapper">
      <div class="order-address" v-if="showAddress">
        <div class="oa-head">
          <span>收货人：{{address.consignee}}</span>
          <span>{{address.phone}}</span>
        </div>
        <div class="oa-desc" @click="_selectAddress">
          <div class="oa-icon-address">
            <Icon type="ios-pin-outline" class="small"/>
          </div>
          <div class="content">收货地址：{{address.realName}}</div>
          <div class="oa-icon-right">
            <Icon type="ios-arrow-forward"/>
          </div>
        </div>
      </div>
      <div class="order-address select-address" @click="_selectAddress" v-else>
        <span>请选择收货地址</span>
        <Icon type="ios-arrow-forward"/>
      </div>
      <!--<ul class="order-list">-->
      <!--<li class="ol-item" v-for="item in goods" :key="item.id">-->
      <!--<div class="ol-img">-->
      <!--<img :src="item.img" width="64" height="80" alt="">-->
      <!--</div>-->
      <!--<div class="ol-desc">-->
      <!--<div class="name">{{item.name}}</div>-->
      <!--<div class="type">颜色分类：黑色</div>-->
      <!--<div class="price-num">-->
      <!--<span class="price">￥{{item.price}}</span>-->
      <!--<span class="num">x{{item.num}}</span>-->
      <!--</div>-->
      <!--</div>-->
      <!--</li>-->
      <!--</ul>-->
      <div class="o-select">
        <div class="select-head">
          <span>优惠券</span>
          <select v-model="select" class="select-s" @change="_changeSelect">
            <option value="">不使用</option>
            <option v-for="item in options" :key="item.id" :value="item.id">{{item.name}}</option>
          </select>
        </div>
        <div class="select-content" v-show="isShow">
          <span>可用数量：{{numCount}}</span>
          <div class="add-delete">
            <div class="ad" @click="minus">
              <Icon type="md-remove"/>
            </div>
            <input type="text" maxlength="11" @input="_handleInput" @blur="_blurInput" class="num" v-model="num">
            <div class="ad" @click="add">
              <Icon type="md-add"/>
            </div>
          </div>
        </div>
      </div>
      <div class="shopping-item" v-for="(item, index) in foodData" :key="item.id" v-if="foodData">
        <div class="shopping-head">
          <div class="sh-left">
            <span class="name">{{item.shopName}}</span>
          </div>
          <!--<div class="sh-right">{{item.shopName}}</div>-->
        </div>
        <ul class="sh-list">
          <li class="l-item" v-for="(food, i) in item.foods" :key="food.id">
            <div class="l-img">
              <img :src="food.goodsLogo" width="64" height="80" alt="">
            </div>
            <div class="l-desc">
              <div class="l-name">{{food.goodsName}}</div>
              <div class="num">
                <span class="price">￥{{food.platformRetailPrice}}</span>
                <span class="num-text">x{{food.goodsCount}}</span>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <div class="order-bottom">
      <div class="ob-left">
        <div class="ol-price">
          <span>合计：</span>
          <span class="ob-price">￥{{_countTotal}}</span>
        </div>
        <div class="fee" v-if="total == undefined">需配送费{{total}}元</div>
      </div>
      <div class="ob-right" @click="_submitFrom">提交订单</div>
    </div>
    <div class="order-hidden" v-show="show">
      <div class="order-address-wrapper">
        <div class="order-address-head">
          <span @click="_remove">取消</span>
          <span class="select">选择收货地址</span>
          <span></span>
        </div>
        <div class="order-address-list">
          <ul>
            <li class="order-address-item" v-for="item in addresseds" :key="item.id" @click="_setAddress(item)">
              <div class="order-address-icon">
                <Icon type="ios-radio-button-off"/>
              </div>
              <div class="order-address-desc">
                <div class="order-address-header">
                  <span class="name">{{item.consignee}}</span>
                  <span class="phone">{{item.phone}}</span>
                </div>
                <div class="order-address-content">
                  {{item.realName}}
                </div>
              </div>
              <div class="order-address-icon" @click="e_editAddress(item.id)">
                <Icon type="ios-create-outline"/>
              </div>
            </li>
          </ul>
        </div>
        <div class="order-address-footer" @click="_goAddAddress">
          <Icon type="ios-add-circle-outline"/>
          <span>新增收货地址</span>
        </div>
      </div>
      <div class="supernatant" @click.prevent="_remove"></div>
    </div>
    <div class="pay" v-show="payShow">
      <div class="pay-wrapper">
        <Icon class="pay-success" type="ios-checkmark-circle-outline"/>
        <div>支付成功</div>
      </div>
    </div>
  </div>
</template>

<script>
  import navbar from '@c/navbar/navbar'
  import { mapState, mapMutations } from 'vuex'
  import { tokenPost, jtpost, gtpost, tokenLink, jtget } from '@api/axiosByToken'

  export default {
    name: 'Order',
    data() {
      return {
        showAddress: false,
        show: false,
        address: {},
        foodData: [],
        addresseds: [],
        total: 0,
        payShow: false,
        options: [],
        select: '',
        num: 0,
        isShow: false,
        qspMax: 0,//轻奢品
        spMax: 0,//商品
        numCount: 0,//可用数量
      }
    },
    components: {
      'v-navbar': navbar,
    },
    computed: {
      ...mapState(['wtitle', 'rback']),
      _countTotal() {
        let count = 0
        this.foodData.forEach(item => {
          item.foods.forEach(item => {
            count += item.goodsCount * item.platformRetailPrice + item.postage
            this.total += item.postage
          })
        })
        if (this.num >= this.numCount) {
          return count - this.numCount
        }
        return count - this.num
      },
    },
    methods: {
      ...mapMutations(['WTITLE', 'RBACK', 'REVISE', 'LOADDING']),
      //收货地址列表
      _selectAddress() {
        this.show = true
        this._getAddress()
      },
      //取消
      _remove() {
        this.show = false
      },
      //获得收货地址
      _getAddress() {
        let getData = {
          userId: localStorage.getItem('userId'),
        }
        this.$store.state.loadding = true
        tokenPost(tokenLink.address.getAddressAll, getData).then(re => {
          this.$store.state.loadding = false
          if (re.status == 0) {
            console.log(re)
            this.addresseds = re.data
          }
        })
      },
      //跳转添加收货地址页面
      _goAddAddress() {
        this.$router.push('/addAddress')
      },
      // 跳转修改收货地址
      e_editAddress(i) {
        console.log(i)
        this.$router.push({
          path: 'addAddress',
          name: 'addAddress',
          params: {
            addressId: i,
          },
        })
      },
      //选择收货地址
      _setAddress(item) {
        this.address = item
        this.show = false
        this.showAddress = true
      },
      _getDefaultAddress() {
        let getData = {
          userId: localStorage.getItem('userId'),
        }
        this.$store.state.loadding = true
        tokenPost(tokenLink.address.getAddress, getData).then(re => {
          this.$store.state.loadding = false
          if (re.status === 0) {
            if (re.data === undefined) {
              this.showAddress = false
              return
            }
            this.address = re.data
            this.showAddress = true
          }
        })
      },
      //获得确认订单列表
      _getGoodsList() {
        if (this.$store.state.orderId === '') {
          this.$router.push('/ShoppingCart')
          return
        }
        let info = {
          userId: window.localStorage.getItem('userId'),
          ids: this.$store.state.orderId,
        }
        this.$store.state.loadding = true
        tokenPost(tokenLink.cart.goods, info).then(re => {
          this.$store.state.loadding = false
          if (re.status === 0) {
            this.foodData = this._manageShoppingCartList(re.data.resultList)
            this.qspMax = re.data.qspMax
            this.spMax = re.data.spMax
            this.options = re.data.xlMap
          } else {
            this.$Message.error(re.msg)
          }
        })
      },
      //购物车列表数据处理
      _manageShoppingCartList(list) {
        let arr = []
        list.forEach(item => {
          let obj = {}
          for (let key in item) {
            obj['shopName'] = item[key][0].businessName
            obj['check'] = false
            item[key].forEach(i => {
              i['sonCheck'] = false
            })
            obj['foods'] = item[key]
          }
          arr.push(obj)
        })
        return arr
      },
      //提交订单
      _submitFrom() {
        if (this.num > this.numCount) {
          this.$Message.error('优惠券数量大于可用数量,请从新输入~~~')
          return
        }
        let info = {
          userId: window.localStorage.getItem('userId'),
          ids: this.$store.state.orderId,
          addressId: this.address.id,
          type: this.select,
          countNumber: this.num,
        }
        this.$store.state.loadding = true
        tokenPost(tokenLink.cart.balance, info).then(re => {
          this.$store.state.loadding = false
          if (re.status === 0) {
            this.payShow = true
            setTimeout(() => {
              this.$store.state.orderId = ''
              this.$store.state.auctionIndex = 3
              this.$router.push('/auction')
            }, 1000)
          } else {
            this.$Message.error(re.msg)
          }
        })
      },
      //加
      add() {
        if (this.num >=this.numCount) {
          this.num = this.numCount
          this.$Message.info('该商品不能增加了呦~')
          return
        }
        this.num++
      },
      //减
      minus() {
        if (this.num <= 1) {
          this.$Message.info('该商品不能减少了呦~')
          return
        }
        this.num--
      },
      /**
       * 选择
       * @private
       */
      _changeSelect() {
        if (this.select === '') {
          this.isShow = false
        } else {
          this.isShow = true
          if (this.select === '3') {
            this.numCount = this.qspMax
          } else {
            this.numCount = this.spMax
          }
        }
      },
      /**
       * 输入验证
       * @param val
       * @private
       */
      _handleInput(val) {
        if (val.target.value === '') {
          return
        }
        let type = /^[0-9]*[1-9][0-9]*$/
        if (!type.test(val.target.value)) {
          this.num = 0
          return
        }
      },
      /**
       * 失去焦点验证
       * @private
       */
      _blurInput() {
        if (this.num > this.numCount) {
          this.num = this.numCount
        }
      }
    },
    created() {
      this.WTITLE('确认订单')
      this.RBACK(true)
      this._getDefaultAddress()
      this._getGoodsList()
    },
  }
</script>

<style scoped>
  .order {
    width: 100%;
    height: 100%;
    padding: 45px 0 50px;
    overflow: auto;
    background: #f6f6f6;
  }

  .order .order-wrapper .order-address {
    background: #ffffff;
  }

  .order .order-wrapper .select-address {
    padding: 15px 19px 10px;
    font-size: 18px;
  }

  .order .order-wrapper .order-address .oa-head {
    display: flex;
    height: 45px;
    align-items: center;
    justify-content: space-between;
    padding: 10px 30px 0 35px;
    font-size: 13px;
    color: #202020;
  }

  .order .order-wrapper .order-address .oa-desc {
    display: flex;
    padding-bottom: 16px;
  }

  .order .order-wrapper .order-address .oa-desc .oa-icon-address {
    flex: 0 0 35px;
    width: 35px;
    font-size: 0;
    text-align: center;
    vertical-align: top;
  }

  .order .order-wrapper .order-address .oa-desc .oa-icon-address .small {
    font-size: 20px;
  }

  .order .order-wrapper .order-address .oa-desc .content {
    flex: 1;
    font-size: 13px;
    color: #202020;
  }

  .order .order-wrapper .order-address .oa-desc .oa-icon-right {
    padding: 0 8px 0 10px;
    font-size: 12px;
  }

  .order .order-bottom {
    position: fixed;
    display: flex;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 50px;
    align-items: center;
    background: #ffffff;
  }

  .order .order-bottom .ob-left {
    padding: 7px 8px 0;
    height: 100%;
    flex: 1;
    box-sizing: border-box;
    border-top: 1px solid #D7D7D7;
  }

  .order .order-bottom .ob-left .ol-price {
    font-size: 12px;
    color: #202020;
  }

  .order .order-bottom .ob-left .ol-price .ob-price {
    color: #B30707;
  }

  .order .order-bottom .ob-left .fee {
    font-size: 10px;
    color: #8C8B8B;
  }

  .order .order-bottom .ob-right {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 111px;
    width: 111px;
    height: 100%;
    background: #FC414E;
    font-size: 16px;
    color: #ffffff;
    font-weight: bold;
  }

  .order .order-hidden {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;
  }

  .order .order-hidden .supernatant {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: rgba(0, 0, 0, .5);
  }

  .order .order-hidden .order-address-wrapper {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 60%;
    overflow: hidden;
    background: #ffffff;
    z-index: 10;
  }

  .order .order-hidden .order-address-wrapper .order-address-head {
    position: absolute;
    top: 0;
    left: 0;
    display: flex;
    width: 100%;
    height: 45px;
    padding: 0 20px;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    color: #7E7E7E;
    background: #ffffff;
    border-bottom: 1px solid RGBA(148, 147, 147, .2);
  }

  .order .order-hidden .order-address-wrapper .order-address-head .select {
    font-size: 14px;
    color: #202020;
  }

  .order .order-hidden .order-address-wrapper .order-address-list {
    width: 100%;
    height: 100%;
    overflow: auto;
    padding: 50px 0;
  }

  .order .order-hidden .order-address-wrapper .order-address-item {
    display: flex;
    align-items: center;
    padding: 5px 10px;
  }

  .order .order-hidden .order-address-wrapper .order-address-item .order-address-icon {
    font-size: 20px;
    color: #BDBCBC;
  }

  .order .order-hidden .order-address-wrapper .order-address-item .order-address-desc {
    flex: 1;
    margin-left: 8px;
  }

  .order .order-hidden .order-address-wrapper .order-address-item .order-address-desc .order-address-header {
    font-size: 14px;
    color: #202020;
  }

  .order .order-hidden .order-address-wrapper .order-address-item .order-address-desc .order-address-header .phone {
    font-size: 12px;
    color: #7E7E7E;
  }

  .order .order-hidden .order-address-wrapper .order-address-item .order-address-content {
    font-size: 12px;
    color: #202020;
  }

  .order .order-hidden .order-address-wrapper .order-address-footer {
    display: flex;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 45px;
    justify-content: center;
    align-items: center;
    font-size: 20px;
    border-top: 1px solid RGBA(148, 147, 147, .2);
    background: #ffffff;
  }

  .order .order-hidden .order-address-wrapper .order-address-footer > span {
    margin-left: 5px;
    font-size: 14px;
  }

  .order .order-wrapper .shopping-item {
    padding: 0 19px 25px;
    margin-top: 10px;
    background: #ffffff;
  }

  .order .order-wrapper .shopping-item .shopping-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 45px;
    border-bottom: 1px solid RGBA(148, 147, 147, 0.2);
    font-size: 0;
  }

  .order .order-wrapper .shopping-item .shopping-head .sh-icon {
    padding: 0 7px;
  }

  .order .order-wrapper .shopping-item .shopping-head .name {
    font-size: 13px;
    color: #202020;
  }

  .order .order-wrapper .shopping-item .shopping-head .sh-right {
    margin-right: 7px;
    font-size: 12px;
    color: #656565;
  }

  .order .order-wrapper .shopping-item .l-item {
    display: flex;
    align-items: center;
    padding-top: 13px;
    font-size: 0;
  }

  .order .order-wrapper .shopping-item .l-img {
    border: 1px solid #cfcfcf;
    font-size: 0;
  }

  .order .order-wrapper .shopping-item .l-desc {
    flex: 1;
    padding-left: 10px;
    vertical-align: top;
  }

  .order .order-wrapper .shopping-item .l-desc .l-name {
    font-size: 13px;
    color: #202020;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .order .order-wrapper .shopping-item .l-desc .num {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 10px;
    margin-top: 13px;
  }

  .order .order-wrapper .shopping-item .l-desc .num .price {
    font-size: 12px;
    color: #a40509;
  }

  .order .order-wrapper .shopping-item .l-desc .num .num-text {
    font-size: 14px;
    color: #202020;
  }

  .order .pay {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .order .pay .pay-wrapper {
    display: flex;
    width: 100px;
    height: 100px;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    background: rgba(0, 0, 0, .5);
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    color: #ffffff;
    font-size: 16px;
  }

  .order .pay .pay-wrapper .pay-success {
    font-size: 50px;
    color: #19be6b;
  }

  .order .o-select {
    margin-top: 10px;
    background-color: #ffffff;
  }

  .order .o-select .select-head {
    display: flex;
    padding: 0 19px;
    justify-content: space-between;
    align-items: center;
    height: 45px;
  }

  .order .o-select .select-head .select-s {
    width: 200px;
    height: 100%;
    text-align-last: right;
    background-color: #ffffff;
  }

  .order .o-select .select-head .select-s > option {
    direction: rtl;
  }

  .order .o-select .select-content {
    display: flex;
    height: 50px;
    padding: 0 19px;
    align-items: center;
    border-top: 1px solid RGBA(148, 147, 147, 0.2);
    justify-content: space-between;
  }

  .add-delete {
    display: flex;
    font-size: 0;
  }

  .add-delete .ad {
    flex: 0 0 24px;
    width: 24px;
    height: 24px;
    font-size: 12px;
    text-align: center;
    background: #F3F3F3;
    color: #CFCFCF;
    border: 1px solid #CFCFCF;
  }

  .add-delete .num {
    display: table-cell;
    vertical-align: middle;
    width: 60px;
    height: 24px;
    line-height: 24px;
    transform: scale(1);
    transform-origin: 0% 0%;
    border: solid #CFCFCF;
    border-width: 1px 0 1px 0;
    text-align: center;
    font-size: 12px;
  }
</style>
